#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using Karma.Framework.Core.Repository.Configuration;

#endregion

namespace Karma.Framework.Core.Domain
{
    public class EnumDescriptor
    {
        protected static IDictionary<string, IDictionary<int, string>> Descriptions =
            new Dictionary<string, IDictionary<int, string>>();

        public static Type[] GetEnums()
        {
            return ActiveRecordConfiguration.Enumerations.Values.ToArray();
        }

        public static IDictionary<int, string> GetDescriptions(string enumName)
        {
            if (Descriptions.ContainsKey(enumName))
            {
                return Descriptions[enumName];
            }

            if (ActiveRecordConfiguration.Enumerations.ContainsKey(
                enumName))
            {
                var enumType = ActiveRecordConfiguration.Enumerations[enumName];
                var enumValues = Enum.GetValues(enumType);
                var keyPairs = new Dictionary<int, string>();
                foreach (var value in enumValues)
                {
                    keyPairs.Add(Convert.ToInt32(value), Enum.GetName(enumType, value));
                }

                Descriptions.Add(enumName, keyPairs);
                return keyPairs;
            }

            throw new ArgumentException(
                string.Format(
                    "Enumeracion no configurada {0}",
                    enumName));
        }
    }
}